package test;

import org.junit.Test;

import search.SEE;
import state.GameState;

public class XRAYTEST {

 
	/*@Test 
	public void SEETestQueenBehindBishop()
	{
		String pos="r1bqk2r/ppp2ppp/2np1n2/2b1p3/2B1P3/1QP2N2/PP1P1PPP/RNB1K2R w KQkq - 0 1";
		GameState gs = new GameState(pos);
		
		// xrayBishopAttacks(long occ, long blockers, int bishopSq) 
		
		for (int i=0; i < 64; i++) {
			long xray = BitboardAttacks.xrayBishopAttacks( gs.bitboard[GameState.ALLPIECES], gs.bitboard[GameState.WB], i) ;
		
			int idx = Long.numberOfTrailingZeros(xray);
			while (idx < 64) {
				int victim= gs.board[63-i];
				int attacker = gs.board[63-idx];
				
				if (victim == -1 || attacker == -1) {
					xray = xray & ( xray - 1 );
					idx = Long.numberOfTrailingZeros(xray);
					continue;
				}
				if (victim % 2 == 0 ){
					xray = xray & ( xray - 1 );
					idx = Long.numberOfTrailingZeros(xray);
					continue;
				}
				
				System.out.println(63 - i + "   victim = " + victim + " idx = " + idx +  "      attacker " + attacker);
				xray = xray & ( xray - 1 );
				idx = Long.numberOfTrailingZeros(xray);
				
			}
			
		}
	}*/
		@Test 
		public void SEETestRookBehindQueen()   // white attackers hava a Q,R,R battery on a knight; defenders have two pawns
		//defending the knight and a queen xray defender behind the pawn
		{
			String pos="r1bqk1nr/ppp2ppp/1n1p4/2b1p3/4P3/1QP5/PR1P1PPP/1RB1KBN1 w kq - 0 1";
			GameState gs = new GameState(pos);
			int side=GameState.WHITE;
			
			long all = gs.bitboard[GameState.ALLPIECES];
			long rooks = gs.bitboard[GameState.WR+side];
			long queens = gs.bitboard[GameState.WQ+side];
		
			long[] rookBlockers={rooks, queens };
			int victimSquare=46;  // or 17
			int attackingColor=GameState.WHITE;
			
			int[] xRayAttacker = SEE.getXRayAttackerRook(gs, all, victimSquare,rookBlockers, attackingColor);
			
			if (xRayAttacker[0] != -1 ) {   
				System.out.println("Xray attacker = " + xRayAttacker[0]);
				System.out.println("Xray blocker =  " + xRayAttacker[1]);
			}
			
			//Now remove the queen so that we have a two rook battery
			String pos2 = "r1bqk1nr/ppp2ppp/1n1p4/2b1p3/4P3/2P5/PR1P1PPP/1RB1KBN1 w kq - 0 1";
			gs = new GameState(pos2);
			
			
			all = gs.bitboard[GameState.ALLPIECES];
			rooks = gs.bitboard[GameState.WR+side];
			queens = gs.bitboard[GameState.WQ+side];
		
			rookBlockers[0]=rooks;
			rookBlockers[1]=queens;
		
			xRayAttacker = SEE.getXRayAttackerRook(gs, all, victimSquare,rookBlockers, attackingColor);

			if (xRayAttacker[0] != -1 ) {   
				System.out.println("Xray attacker = " + xRayAttacker[0]);
				System.out.println("Xray blocker =  " + xRayAttacker[1]);
			}
			
		   String pos3="rnb1k1nr/pp1p1ppp/1qp5/2b1p3/2B1P3/1QP5/PP1P1PPP/RNB1K1NR w KQkq - 0 1";
		   gs = new GameState(pos3);
		   all = gs.bitboard[GameState.ALLPIECES];
		   queens = gs.bitboard[GameState.WQ+side];
		   long bishops = gs.bitboard[GameState.WB+side];
		   
		   long[] blockers={ bishops, queens };
		   victimSquare=50;  // or 13
		   
		   xRayAttacker = SEE.getXRayAttackerBishop(gs, all, victimSquare,blockers, attackingColor);

			if (xRayAttacker[0] != -1 ) {   
				System.out.println("Xray attacker = " + xRayAttacker[0]);
				System.out.println("Xray blocker =  " + xRayAttacker[1]);
			}
		   String pos4="rnbq1knr/pp1p1Ppp/2p5/2b1p3/2B1P3/2P5/PP3PP1/RNB1K1NR w KQ - 0 1";
		   victimSquare=57;  // or 6
		   gs = new GameState(pos4);
		   all = gs.bitboard[GameState.ALLPIECES];
		   queens = gs.bitboard[GameState.WQ+side];
		   bishops = gs.bitboard[GameState.WB+side];
		 
		   long[] blockers2={bishops, queens };
		   
		   xRayAttacker = SEE.getXRayAttackerBishop(gs, all, victimSquare,blockers2, attackingColor);
		   if (xRayAttacker != null) {
			   if (xRayAttacker[0] != -1 ) {   
					System.out.println("Xray attacker = " + xRayAttacker[0]);
					System.out.println("Xray blocker =  " + xRayAttacker[1]);
				}
		   }
			
	}
		
	
}
